package com.hxq.library.controller;

import com.hxq.library.service.IUserService;
import com.hxq.library.service.Impl.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @Author: Ljx
 * @Date: 2021/11/24 21:45
 * @role:
 */
@WebServlet("/pay")
public class PayServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;utf-8");
        String account = req.getParameter("account");
        String num = req.getParameter("num");
        String paw = req.getParameter("paw");
        int money = Integer.parseInt(req.getParameter("money"));

        if (account==""||num==""||paw==""){
            req.setAttribute("msg2","输入的值不能为空！！！");
            req.getRequestDispatcher("pay.jsp").forward(req,resp);
        }else {
            IUserService service = new UserService();
            boolean pay = false;
            try {
                pay = service.pay(money, account, paw, num);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            if (pay){
                req.setAttribute("msg2","恭喜你支付成功！！！");
                req.getRequestDispatcher("pay.jsp").forward(req,resp);
            }else {
                req.setAttribute("msg2","账号或密码错误支付失败！！！");
                req.getRequestDispatcher("pay.jsp").forward(req,resp);
            }
        }
    }
}
